package com.example.polardbxdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.polardbxdemo.entity.Material;
import com.example.polardbxdemo.mapper.MaterialMapper;
import com.example.polardbxdemo.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Override
    public List<Material> getByType(Integer type) {
        log.info("根据类型查询物料: type={}", type);
        return baseMapper.selectByType(type);
    }

    @Override
    public List<Material> getByPropertyCategory(String category) {
        log.info("根据JSON属性分类查询物料: category={}", category);
        return baseMapper.selectByPropertyCategory(category);
    }

    @Override
    public boolean addMaterial(Material material) {
        log.info("添加物料: name={}, code={}", material.getName(), material.getCode());
        return save(material);
    }

    @Override
    public boolean updateMaterialProperties(Long id, Map<String, Object> properties) {
        log.info("更新物料属性: id={}, properties={}", id, properties);
        Material material = getById(id);
        if (material != null) {
            material.setProperties(properties);
            return updateById(material);
        }
        return false;
    }

    @Override
    public List<Material> getByTypeOrderByCreateTime(Integer type) {
        log.info("根据类型查询物料并按创建时间排序: type={}", type);
        return baseMapper.selectByTypeOrderByCreateTime(type);
    }

    @Override
    public List<Material> getByPriceRange(Double minPrice, Double maxPrice) {
        log.info("查询价格范围内的物料: minPrice={}, maxPrice={}", minPrice, maxPrice);
        return baseMapper.selectByPriceRange(minPrice, maxPrice);
    }

    @Override
    public List<Material> getByNameLike(String name) {
        log.info("根据名称模糊查询物料: name={}", name);
        return baseMapper.selectByNameLike(name);
    }

    @Override
    public List<Material> getEnabledMaterials() {
        log.info("查询所有启用的物料");
        return baseMapper.selectEnabledMaterials();
    }

    @Override
    public List<Material> getByTypeAndStock(Integer type, Integer minStock) {
        log.info("根据类型和库存查询: type={}, minStock={}", type, minStock);
        return baseMapper.selectByTypeAndStock(type, minStock);
    }

    @Override
    public List<Material> complexQuery(String name, Integer type, Double minPrice, Double maxPrice, Boolean enabled) {
        log.info("复杂条件查询: name={}, type={}, minPrice={}, maxPrice={}, enabled={}",
                name, type, minPrice, maxPrice, enabled);

        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !name.isEmpty(), Material::getName, name)
                .eq(type != null, Material::getType, type)
                .ge(minPrice != null, Material::getPrice, minPrice)
                .le(maxPrice != null, Material::getPrice, maxPrice)
                .eq(enabled != null, Material::getEnabled, enabled)
                .eq(Material::getDeleted, 0)
                .orderByDesc(Material::getCreateTime);

        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 使用 Service 层的 lambdaQuery 方法（更简洁的方式）
     */
    public List<Material> lambdaQueryExample(String keyword) {
        return lambdaQuery()
                .and(wrapper -> wrapper
                        .like(Material::getName, keyword)
                        .or()
                        .like(Material::getCode, keyword)
                )
                .eq(Material::getEnabled, true)
                .eq(Material::getDeleted, 0)
                .gt(Material::getStock, 0)
                .orderByDesc(Material::getPrice)
                .list();
    }

    /**
     * 更新库存数量
     */
    public boolean updateStock(Long id, Integer newStock) {
        log.info("更新库存: id={}, newStock={}", id, newStock);

        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getId, id)
                .eq(Material::getDeleted, 0);

        Material material = new Material();
        material.setStock(newStock);

        return update(material, queryWrapper);
    }

    /**
     * 批量更新启用状态
     */
    public boolean batchUpdateEnabled(List<Long> ids, Boolean enabled) {
        log.info("批量更新启用状态: ids={}, enabled={}", ids, enabled);

        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Material::getId, ids)
                .eq(Material::getDeleted, 0);

        Material material = new Material();
        material.setEnabled(enabled);

        return update(material, queryWrapper);
    }
}